package com.reserv.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import com.reserv.db.ConnectionProvider;
import com.reserv.db.JDBCManager;
import com.reserv.vo.ZipCodeVO;

public class ZipCodeDAO implements DAO<ZipCodeVO, String> {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	private static ZipCodeDAO instance = new ZipCodeDAO();
	public static ZipCodeDAO getInstance() {
		return instance != null ? instance : new ZipCodeDAO();
	}
	public List<ZipCodeVO> getListVOs(String dong) {
		String sql = "SELECT * FROM zipcode WHERE dong LIKE ?";
		List<ZipCodeVO> list = null;
		ZipCodeVO zc = null;
		try {
			conn = ConnectionProvider.getConnection();
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, "%" + dong + "%");
			
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				list = new ArrayList<>();
				do {
					zc = new ZipCodeVO();
					zc.setZipCode(rs.getString(1));
					zc.setSido(rs.getString(2));
					zc.setGugun(rs.getString(3));
					zc.setDong(rs.getString(4));
					zc.setRi(rs.getString(5));
					zc.setBunji(rs.getString(6));

					list.add(zc);
				} while(rs.next());
			}
		} catch (NamingException e) {
			System.out.println("NamingException in ZipCodeDAO - getListVOs(String dong)");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("SQLException in ZipCodeDAO - getListVOs(String dong)");
			e.printStackTrace();
		} finally {
			JDBCManager.closeAll(conn, pstmt, rs);
		}
		return list;
	}
	
	@Override
	public void createVO(ZipCodeVO zipCode) {
		
	}
	@Override
	public ZipCodeVO getVO(String zipCode) {
		return null;
	}
	@Override
	public List<ZipCodeVO> getListVOs() {
		return null;
	}
	@Override
	public void updateVO(ZipCodeVO zipCode) {
		
	}
	@Override
	public void deleteVO(String zipCode) {
		
	}
}
